import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:gsy_flutter_app/common/config/Config.dart';
import 'package:gsy_flutter_app/common/config/NetConfig.dart';
import 'package:gsy_flutter_app/common/dao/DataResult.dart';
import 'package:gsy_flutter_app/common/local/LocalStorage.dart';
import 'package:gsy_flutter_app/common/mdoel/User.dart';
import 'package:gsy_flutter_app/common/net/Address.dart';
import 'package:gsy_flutter_app/common/net/Api.dart';
import 'package:gsy_flutter_app/common/net/ResultData.dart';

/// 作者: Tao
/// 日期: 2024/1/18
/// 时间: 13:43
/// 描述: 用户登录
class UserDao {
  /// 方法描述:
  /// [userName] 用户名
  /// [password] 密码
  /// [callback] 回调函数
  /// return: []
  static login(userName, password, callback) async {
    var type = userName + ":" + password;
    var bytes = utf8.encode(type);
    var base64Str = base64.encode(bytes);
    if (Config.DEBUG) {
      print('base64Str login $base64Str');
    }
    //存用户名和加密后的加密字符串
    await LocalStorage.save(Config.USER_NAME_KEY, userName);
    await LocalStorage.save(Config.USER_BASIC_CODE, base64Str);
    //请求参数配置
    Map requestParams = {
      "scopes": ['user', 'repo', 'gist', 'notifications'],
      "note": "admin_script",
      "client_id": NetConfig.CLIENT_ID,
      "client_secret": NetConfig.CLIENT_SECRET
    };

    HttpManager.clearAuthorization();

    ResultData res = await HttpManager.netFetch(Address.getAuthorization(),
        json.encode(requestParams), null, Options(method: "post"));
    if (res.result) {
      await LocalStorage.save(Config.PW_KEY, password);
      DataResult dataResult = await getUserInfo(userName);
      if (Config.DEBUG) {
        print("user result ${dataResult.result.toString()}");
        print(dataResult.data);
        print(res.data.toString());
      }
    }

    if (callback != null) {
      callback(res);
    }
  }

  static initUserInfo() async {
    var token = await LocalStorage.get(Config.TOKEN_KEY);
    var res = await getUserInfoLocal();
    if (res != null && res.result && token != null) {}
    return DataResult(res.data, (res.result && (token != null)));
  }

  /// 方法描述: 获取本地用户信息
  /// return: [DataResult] 包装后的返回数据
  static getUserInfoLocal() async {
    var userInfo = await LocalStorage.get(Config.USER_INFO);
    if (userInfo != null) {
      var userMap = json.decode(userInfo);
      var user = User.fromJson(userMap);
      return DataResult(user, true);
    } else {
      return DataResult(null, false);
    }
  }

  /// 方法描述: 获取用户信息，从github上拉用户信息到本地，并存储在本地
  /// [username] 用户名
  /// return: [DataResult] 从github上拉用户信息到本地的数据
  static getUserInfo(String? username) async {
    ResultData res;
    if (username == null) {
      res =
          await HttpManager.netFetch(Address.getMyUserInfo(), null, null, null);
    } else {
      res = await HttpManager.netFetch(
          Address.getUserInfo(username), null, null, null);
    }
    if (res != null && res.result) {
      User user = User.fromJson(res.data);
      user.starred = "---";
      if (username == null) {
        LocalStorage.save(Config.USER_INFO, json.encode(res.data));
      }
      return DataResult(user, true);
    } else {
      return DataResult(res.data, false);
    }
  }
}
